<?php

namespace Database\Seeders;

use App\Models\Post;
use App\Models\Reply;
use App\Models\Report;
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class ReportSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 获取所有用户、帖子和回复
        $users = User::all();
        $posts = Post::all();
        $replies = Reply::all();

        if ($users->isEmpty() || ($posts->isEmpty() && $replies->isEmpty())) {
            $this->command->warn('没有足够的数据来生成举报记录，请先运行其他 seeder');
            return;
        }

        $reasons = ['spam', 'abuse', 'inappropriate', 'other'];
        $descriptions = [
            'spam' => ['这是垃圾信息', '内容包含大量广告', '重复发布相同内容', '无意义的水贴'],
            'abuse' => ['包含辱骂性语言', '人身攻击', '恶意中伤', '语言不当'],
            'inappropriate' => ['内容不当', '违反社区规范', '不适合公开讨论', '包含敏感信息'],
            'other' => ['其他违规行为', '需要管理员审核', '疑似虚假信息'],
        ];

        $statuses = ['pending', 'processing', 'resolved', 'rejected'];
        $adminComments = [
            '已处理，已删除违规内容',
            '经核实，内容符合规范',
            '举报不成立',
            '已警告发布者',
        ];

        // 获取管理员用户（用于处理过的举报）
        $admins = $users->where('is_admin', true);

        DB::transaction(function () use ($users, $posts, $replies, $reasons, $descriptions, $statuses, $adminComments, $admins) {
            // 生成对帖子的举报（约 10 条）
            $postReportsCount = min(10, $posts->count());
            for ($i = 0; $i < $postReportsCount; $i++) {
                $post = $posts->random();
                $reporter = $users->random();
                
                // 确保举报者不是帖子作者
                if ($reporter->id === $post->user_id) {
                    continue;
                }

                $reason = $reasons[array_rand($reasons)];
                $status = $statuses[array_rand($statuses)];
                
                $report = Report::create([
                    'user_id' => $reporter->id,
                    'reportable_type' => Post::class,
                    'reportable_id' => $post->id,
                    'reason' => $reason,
                    'description' => $descriptions[$reason][array_rand($descriptions[$reason])],
                    'status' => $status,
                    'admin_id' => ($status !== 'pending' && $admins->isNotEmpty()) ? $admins->random()->id : null,
                    'admin_comment' => ($status !== 'pending') ? $adminComments[array_rand($adminComments)] : null,
                    'processed_at' => ($status !== 'pending') ? now()->subDays(rand(1, 30)) : null,
                    'created_at' => now()->subDays(rand(1, 60)),
                    'updated_at' => ($status !== 'pending') ? now()->subDays(rand(1, 30)) : now()->subDays(rand(1, 60)),
                ]);
            }

            // 生成对回复的举报（约 5 条）
            $replyReportsCount = min(5, $replies->count());
            for ($i = 0; $i < $replyReportsCount; $i++) {
                $reply = $replies->random();
                $reporter = $users->random();
                
                // 确保举报者不是回复作者
                if ($reporter->id === $reply->user_id) {
                    continue;
                }

                $reason = $reasons[array_rand($reasons)];
                $status = $statuses[array_rand($statuses)];
                
                $report = Report::create([
                    'user_id' => $reporter->id,
                    'reportable_type' => Reply::class,
                    'reportable_id' => $reply->id,
                    'reason' => $reason,
                    'description' => $descriptions[$reason][array_rand($descriptions[$reason])],
                    'status' => $status,
                    'admin_id' => ($status !== 'pending' && $admins->isNotEmpty()) ? $admins->random()->id : null,
                    'admin_comment' => ($status !== 'pending') ? $adminComments[array_rand($adminComments)] : null,
                    'processed_at' => ($status !== 'pending') ? now()->subDays(rand(1, 30)) : null,
                    'created_at' => now()->subDays(rand(1, 60)),
                    'updated_at' => ($status !== 'pending') ? now()->subDays(rand(1, 30)) : now()->subDays(rand(1, 60)),
                ]);
            }
        });

        $this->command->info('已生成 ' . Report::count() . ' 条举报记录');
    }
}
